//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 张小东 on 16/1/17.
//  Copyright © 2016年 Mr.Zhang. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    private func setUpUI() {
        
        self.view.addSubview(headImageView)
        self.view.addSubview(label)
        // 设置子控件的约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_centerX)
            make.top.equalTo(headImageView.snp_bottom).offset(15)
        }
        //头像圆角设置
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        headImageView.layer.borderWidth = 2.0
        headImageView.layer.borderColor = UIColor.whiteColor().CGColor
        //隐藏文案
        label.alpha = 0
        
        // 显示用户头像
        headImageView.sd_setImageWithURL(UserAccountModel().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    
    //    显示动画效果
    private func showAnimation() {
        // 在动画闭包中修改 头像的底部约束
        // 跟新某一条已经添加过的约束 如果该约束不存在 会自动添加该约束
        // usingSpringWithDamping : 弹簧系数 0.1 ~ 1 越小 约谈  准则: 弹簧稀疏 * 10 ~= 加速度
        // initialSpringVelocity : 加速度
        // options : 动画可选项  OC按位枚举 swift 数组
        // 使用自动布局 + 动画闭包 -----> 强制刷新视图
        let offset = -UIScreen.mainScreen().bounds.height + 160
        UIView.animateWithDuration(1.5, delay: 0.2, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.headImageView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 显示文案
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.label.alpha = 1.0
                    }, completion: { (_) -> Void in
                        print("OK")
//                        发出显示访客视图的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
                        
                })
        }
    }

    
    // 添加欢迎界面的子视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var headImageView: UIImageView = UIImageView()
    private lazy var label: UILabel = UILabel(title: (UserAccountModel().userName ?? "") + " 欢迎归来", fontSize: 16, color: UIColor.darkGrayColor())
    
}
